package edu.bu.cs683.traveldroid;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import edu.bu.cs683.traveldroid.data.PointOfInterest;
import edu.bu.cs683.traveldroid.data.Tour;

/**
 * This class implements the MapActivity that TravelDroid used to display Route,
 * Point Of Interest and Current Location data to the user.
 */
public class MapTest extends MapActivity {
	private static final String CLASS_TAG = MapTest.class.getSimpleName();

	public static final int SATELLITE_MENU_ID = 10;
	public static final int STATS_MENU_ID = 11;
	public static final int PAUSE_MENU_ID = 12;
	public static final int CANCEL_MENU_ID = 13;
	public static final int QUIT_MENU_ID = 14;

	// Map Controls
	MapView mapView;
	MapController controller;

	private Tour currentTour;

	// Location Overlay
	MyLocationOverlay locationOverlay;

	boolean satDisplayed = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.v(CLASS_TAG, "onCreate() Called");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Retrieve MapView and Controller
		mapView = (MapView) findViewById(R.id.mapview);
		controller = mapView.getController();

		// Initialize the Map View
		initializeMap();

		// Test Code
		List<Tour> theTours = Tour.loadTourData(this.getResources());
		currentTour = theTours.get(0);

		// Add the currently selected Tour to the Map
		addTourToMap();
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, SATELLITE_MENU_ID, 0, "Toggle\nSatellite");
		menu.add(0, STATS_MENU_ID, 0, "View\nStats");
		menu.add(0, PAUSE_MENU_ID, 0, "Pause\nTour");
		menu.add(0, CANCEL_MENU_ID, 0, "Cancel\nTour");
		menu.add(0, QUIT_MENU_ID, 0, "Quit\nTravelDroid");
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case SATELLITE_MENU_ID:
			// Enable/Disable Satellite Imagery
			satDisplayed = !satDisplayed;
			mapView.setSatellite(satDisplayed);
			return true;
		case QUIT_MENU_ID:
			finish();
			return true;
		}
		return false;
	}

	/**
	 * Method used to initialize the MapView
	 */
	private void initializeMap() {
		Log.v(CLASS_TAG, "initializeMap() Called");

		// Enable Zoom View and set zoom
		mapView.setBuiltInZoomControls(true);
		controller.setZoom(18);

		// Disable Satellite
		mapView.setSatellite(satDisplayed);

		// Create overlay for Device Location and enable location updates
		locationOverlay = new MyLocationOverlay(this, this.mapView);
		mapView.getOverlays().add(locationOverlay);

		// Compass dosen't seem to be working
		// locationOverlay.enableCompass();

		// Method to run when Fix is acquired
		locationOverlay.runOnFirstFix(new Runnable() {
			public void run() {
				controller.animateTo(locationOverlay.getMyLocation());
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();

		// Enable Location Updates for MyLocationOverlay
		locationOverlay.enableMyLocation();

	}

	@Override
	protected void onPause() {
		super.onPause();

		// Disable Location Updates for MyLocationOverlay
		locationOverlay.disableMyLocation();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 
	 */
	private void addTourToMap() {
		Log.v(CLASS_TAG, "addTourToMap() Called");

		// First add the points of interest
		List<PointOfInterest> pois = currentTour.getPoiList();

		// Overlay List
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(
				R.drawable.mappin_red);
		POIItemizedOverlay itemizedOverlay = new POIItemizedOverlay(drawable,
				pois, this);

		// Save start point
		GeoPoint startPoint = null;

		// Create an overlay for each POI
		for (PointOfInterest poi : pois) {
			if (startPoint == null) {
				startPoint = poi.getPoint();
			}

			// Create Overlay for POI
			OverlayItem overlayitem = new OverlayItem(poi.getPoint(), poi
					.getName(), null);

			// Add the overlay item
			itemizedOverlay.addOverlay(overlayitem);

		}

		mapOverlays.add(itemizedOverlay);

		// move to location
		if (startPoint != null) {
			mapView.getController().animateTo(startPoint);
		}

		// Add the route to the map
		RouteOverlay routeOverlay = new RouteOverlay(currentTour
				.getRoutePoints());
		mapOverlays.add(routeOverlay);

		// redraw map
		mapView.postInvalidate();
	}
}